package com.a21vianet.quincysx.demo.contentprovider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.a21vianet.quincysx.demo.contentprovider.bean.Book;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_add:
                add();
                break;
            case R.id.btn_get:
                query();
                break;
            default:
        }
    }

    private void query() {
        Uri bookUri = Uri.parse("content://" + BookContentProvider.AUTHORITY + "/book");
        Cursor query = getContentResolver().query(bookUri, new String[]{"_id", "name", "price"},
                null, null, null);
        while (query.moveToNext()) {
            Book book = new Book();
            book.setId(query.getInt(0));
            book.setName(query.getString(1));
            book.setPrice(query.getDouble(2));
            Log.e("==数据==", book.toString());
        }
        query.close();
    }

    private void add() {
        Uri bookUri = Uri.parse("content://" + BookContentProvider.AUTHORITY + "/book");
        ContentValues values = new ContentValues();
        values.put("name", "Android");
        values.put("price", "39.9");
        getContentResolver().insert(bookUri, values);
    }
}
